package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sRechargeRecord struct{}

var (
	insRechargeRecord = sRechargeRecord{}
)

func RechargeRecord() *sRechargeRecord {
	return &insRechargeRecord
}

// RechargeRecordList 获取充值/赠送金额记录列表
func (s *sRechargeRecord) RechargeRecordList(ctx context.Context, req *model.SearchRechargeRecordList) (res *model.RechargeRecordListPageRes, err error) {

	r := g.RequestFromCtx(ctx)
	startNum, endNum := paging.Paging(req.Paging)
	findList := make([]*model.RechargeRecordListRes, 0)
	searchList := g.Map{
		"user_id":   req.UserId,
		"give_type": req.GiveType,
		"id":        req.Id,
	}
	if err = dao.LotRechargeRecord.Ctx(ctx).OmitEmpty().WithAll().Where(searchList).OrderDesc("created_at").Limit(startNum, endNum).Scan(&findList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	// 获取总条数
	countInfo, err := dao.LotRechargeRecord.Ctx(ctx).OmitEmpty().Where(searchList).Count()
	putCount := paging.PutCount(req.Paging, countInfo)
	res = &model.RechargeRecordListPageRes{
		List:    findList,
		PutPage: putCount,
	}
	return res, err
}
